<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12"><span class="upload-text">店面图片上传</span></el-col>
      <el-col :span="12"><span class="upload-text">图片效果预览</span></el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-upload
                class="upload-demo"
                drag
                :limit="limit"
                :action="uploadUrl"
                :on-success="uploadSucc"
                :on-remove="handleRemove"
                :on-exceed = "onExceed">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-col>
      <el-col :span="12">
        <el-image style="width: 300px; height: 200px" :src="form.pic" fit="contain"></el-image>
      </el-col>
    </el-row>
    <el-row :gutter="20" style=" margin-top: 50px">
      <el-col :span="12">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="店铺名称" prop="name">
            <el-input v-model="form.name" clearable></el-input>
          </el-form-item>
          <el-form-item label="店铺介绍" prop="detail">
            <el-input v-model="form.detail" clearable></el-input>
          </el-form-item>
          <el-form-item label="经营/停营" prop="state">
            <el-switch v-model="state" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">完成</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: "StoreDesign",
    data(){
      return{
        uploadUrl: this.$store.state.takeaway + '/upload',
        limit: 1,
        state: false,
        form:{
          name: "",
          pic: "",
          detail: "",
          state: "",
        },
        rules: {
          name: [
            {required: true, message: '请输入商品名称', trigger: 'blur'},
            {min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur'},
          ],
          detail:[
            {max:50,message:'最多 50 个字符',trigger:'blur'},
          ],
        }
      }
    },
    methods:{
      uploadSucc(res){
        console.log("上穿图片名称："+res);
        this.form.pic = this.$store.state.getImage + res;
      },
      handleRemove(file){
        this.$axios.post("/delFile",{
          path:file.response
        }).then(res=>{
          console.log(res);
          this.form.pic = ""
        }).catch(err=>{
          console.log(err)
        })
      },
      onExceed(files, fileList){
        this.$message.error("提示：只能上传一张图片！")
      },
      onSubmit(form){
        this.$refs[form].validate((value)=>{
          if(value){
            if(this.state){
              this.form.state = "经营"
            }else{
              this.form.state = "关闭"
            }
            const data = this.form;
            data['userId'] = this.$store.state.userId;
            this.$axios.post('/changeStore',data)
              .then(res=>{
                if(res.data.code == 200){
                  this.$message.success(res.data.message)
                }else{
                  this.$message.error(res.data.message)
                }
              }).catch(err=>{
              console.log(err)
            })
          }else{
            console.log('error submit!!');
            return false;
          }
        })
      }
    },
    mounted() {
      const userId = this.$store.state.userId;
      this.$axios.post('/findStore',{
        userId
      }).then(res=>{
        // console.log(res);
        this.form = res.data;
        if(this.form.state == "经营"){
          this.state = true;
        } else {
          this.state = false;
        }
      }).catch(err=>{
        console.log(err);
      })
    }
  }
</script>

<style scoped>

</style>